'use strict';

const Controller = require('egg').Controller;
class EssayController extends Controller {

  async alpage() {
    const { ctx } = this;
    let res = { "code": 1, "msg": "成功" };
    const data = this.ctx.request.query.arid;
    const result = await this.app.mysql.query(`SELECT * , username, avater from articles, 
                                        users where aid = ${data} and articles.uid = users.uid`);

    console.log(result);
    res.aInfo = result;
    res.username = ctx.session.username;
    ctx.body = res;

  }
  //点赞
  async supp() {
    const { ctx } = this;
    let res = { "code": 0, "msg": "成功" };
    const dianzan = ctx.request.query;
    const asa = `INSERT INTO supports (aid,uid) VALUE (${dianzan.aid},${dianzan.uid})`;
    let results = await this.app.mysql.query(asa);
    const upart = `UPDATE articles SET supports=supports+1 WHERE aid=${dianzan.aid};`
    results = await this.app.mysql.query(upart);
    console.log(results);
    res = results;

    ctx.body = dianzan;

  }
  //取消点赞
  async del() {
    const { ctx } = this;
    let res = { "code": 0, "msg": "成功" };
    const dianzan = ctx.request.query;
    const asa = ` DELETE FROM supports WHERE uid=${dianzan.uid} AND aid=${dianzan.aid};`;
    let results = await this.app.mysql.query(asa);
    const upart = `UPDATE articles SET supports=supports-1 WHERE aid=${dianzan.aid};`
    results = await this.app.mysql.query(upart);
    console.log(results);
    res = results;

    ctx.body = dianzan;
  }
  async supp1() {
    const { ctx } = this;
    let res = { "code": 0, "msg": "成功" };
    // ctx.session.aid = 9;
    const aa = ctx.request.query;
    const results = await this.app.mysql.select('supports', { // 搜索 post 表
      where: { uid: aa.uid, aid: aa.aid }, // WHERE 条件
      columns: ['uid'], // 要查询的表字段
    });
    // SELECT * from supprts where uid=1 AND aid=9;
    console.log(results);
    res = results;
    ctx.body = res;
  }


}

module.exports = EssayController;
